package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.ArticleType;
import cn.itsource.domain.Employee;
import cn.itsource.domain.Role;
import cn.itsource.query.EmployeeQuery;
import cn.itsource.service.IEmployeeService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "文章接口")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 增加/修改
     * @param
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="employee:add",name="新增/修改员工信息",descs = "新增/修改员工信息")
    public R addORupdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="employee:del",name="删除员工信息",descs = "删除员工信息")
    public R del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="employee:delList",name="批量删除员工信息",descs = "批量删除员工信息")
    public R delList(@RequestBody List<Long> ids){
        try {
            employeeService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 获取单个
     * @param id
     * @return
     */
    @ApiOperation(value = "获取单个")
    @GetMapping("/{id}")
    @PreAuthorize(sn="employee:get",name="获取单个员工信息",descs = "获取单个员工信息")
    public R getOne(@PathVariable("id") Long id){
        try {
            return R.ok(employeeService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="employee:pageQuery",name="分页查询员工信息",descs = "分页查询员工信息")
    public PageInfo<Employee> pageQuery(@RequestBody EmployeeQuery query){
        return employeeService.pageQuery(query);
    }
}
